const fs = require("fs")
const path =require("path")

/* 解析出静态文件位置 */
const reg =/\/file\/(.*)/
const resoloveFileUrl=(url)=>reg.exec(url)[1]

// 拼接路径
const publicPath=path.join(__dirname,"..","public")

/* /file/img/xxxxxxx */
function handleRequest(req,res){
    // res.writeHead(200, { "Content-Type": "application/json" })
    
    // res.end("文件服务")
    
    // const buffer = fs.readFileSync("D/learn-js-220/作/nodejs原生开发服务/nodej/publi/im/0aff0a42cece09ee.png")
    // const buffer = fs.readFileSync(`D:/learn-js-2207/作业/nodejs原生开发服务端/nodejs/public/${resoloveFileUrl(req.url)}`)
    // res.end(buffer)

    let filePath = `${publicPath}/${resoloveFileUrl(req.url)}`
    console.log("filePath=",filePath);
    const buffer = fs.readFileSync(filePath)
    res.end(buffer)


}

module.exports={
    handleRequest
}